using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbKhachHangDAL
    {
       private SqlConnection cn = null;
        public tbKhachHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbKhachHangDAL(tbKhachHangDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbKhachHang khachhang)
       {
           SqlCommand cm = new SqlCommand("tbKhachHang_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@TenKH",khachhang .TenKH);
           cm.Parameters.AddWithValue("@TaiKhoan", khachhang.TaiKhoan);
           cm.Parameters.AddWithValue("@MatKhau", khachhang.MatKhau);
           cm.Parameters.AddWithValue("@SoDT", khachhang.SoDT);
           cm.Parameters.AddWithValue("@Email", khachhang.Email);
           cm.Parameters.AddWithValue("@DiaChi", khachhang.DiaChi);
           cm.ExecuteNonQuery();
       }
       public void Update(tbKhachHang khachhang)
       {
           SqlCommand cm = new SqlCommand("tbKhachHang_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaKH", khachhang.MaKH);
           cm.Parameters.AddWithValue("@TenKH", khachhang.TenKH);
           cm.Parameters.AddWithValue("@TaiKhoan", khachhang.TaiKhoan);
           cm.Parameters.AddWithValue("@MatKhau", khachhang.MatKhau);
           cm.Parameters.AddWithValue("@SoDT", khachhang.SoDT);
           cm.Parameters.AddWithValue("@Email", khachhang.Email);
           cm.Parameters.AddWithValue("@DiaChi", khachhang.DiaChi);
           cm.ExecuteNonQuery();
       }
       public void Delete(int ID)
       {
           SqlCommand cm = new SqlCommand("tbKhachHang_Detele", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaKH", ID);
           cm.ExecuteNonQuery();

       }
       public tbKhachHangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbKhachHang_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbKhachHangCollection khachhangCollection = new tbKhachHangCollection();
           while (dr.Read())
           {
               tbKhachHang khachhang = new tbKhachHang();
               khachhang.MaKH = int.Parse(dr.GetValue(0).ToString());
               khachhang .TenKH  = dr.GetValue(1).ToString();
               khachhang.TaiKhoan = dr.GetValue(2).ToString();
               khachhang.MatKhau = dr.GetValue(3).ToString();
               khachhang.SoDT = dr.GetValue(4).ToString();
               khachhang.Email = dr.GetValue(5).ToString();
               khachhang.DiaChi = dr.GetValue(6).ToString();
               khachhangCollection.Add(khachhang);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return khachhangCollection;


       }
    }
}
